﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace EnterpriseFund.Controllers
{
    public class QuerySpecificAdjustController : Controller
    {
        //
        // GET: /QuerySpecificAdjust/
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        public ActionResult Print()
        {
            GetWhereString();
            // CreatReport(table);
            return View("Print");
        }
        public ActionResult ReportViewerPartial()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.SpecificAdjust().GetModelQueryList(strWhere);
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QuerySpecificAdjust") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {

           for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {

                if (e.ColumnsInfo[i].FieldName == "NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth = 170;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth = 90;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth =320;

                }
                if (e.ColumnsInfo[i].FieldName == "FSpecialAdjustName")
                {
                    e.ColumnsInfo[i].ColumnWidth =120 ;
                }
                if (e.ColumnsInfo[i].FieldName == "FEmpName")
                {
                    e.ColumnsInfo[i].ColumnWidth =  100;
                }
                if (e.ColumnsInfo[i].FieldName == "FAdjustAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth = 140;
                }
       if (e.ColumnsInfo[i].FieldName == "FInsDateStr")
                {
                    e.ColumnsInfo[i].ColumnWidth = 120;
                }

            }
          
          
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            if (e.FieldName == "FAdjustAmount")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;

            }
            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            
        }
       
        public string GetWhereString()
        {
            //绑定值
            var FSpecialAdjustName = IsNull(ComboBoxExtension.GetValue<string>("FSpecialAdjustName"), Request["FSpecialAdjustName"]);
            var nsrsbh = Request["nsrsbh"];
            var nsrbm = Request["nsrbm"];
            var nsrmc = Request["nsrmc"];
            var rKRQBeginDate = Request["RKRQBeginDate"];
            var rKRQEndDate = Request["RKRQEndDate"];
            var sSRQBeginDate = Request["SSRQBeginDate"];
            var sSRQEndDate = Request["SSRQEndDate"];

             ViewData["FSpecialAdjustName"] = FSpecialAdjustName;
             ViewData["nsrsbh"] = nsrsbh;
             ViewData["nsrbm"] = nsrbm;
             ViewData["nsrmc"] = nsrmc;
             ViewData["RKRQBeginDate"] = rKRQBeginDate;
             ViewData["RKRQEndDate"] = rKRQEndDate;
             ViewData["SSRQBeginDate"] = sSRQBeginDate;
             ViewData["SSRQEndDate"] = sSRQEndDate;
            var codenameBll = new BLL.SpecialAdjustCls();
            ViewBag.FSpecialAdjustNames = codenameBll.GetModelList1("1=1");
            var filter = "1=1";
            if (!string.IsNullOrEmpty(nsrsbh))
                filter += string.Format("and nsrsbh like  '%{0}%'", nsrsbh);
            if (!string.IsNullOrEmpty(nsrbm))
                filter += string.Format("and nsrbm like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(nsrmc))
                filter += string.Format("and nsrmc like '%{0}%'", nsrmc);


            if (!string.IsNullOrEmpty(FSpecialAdjustName))
            {
                filter += string.Format(" and fund.t_SpecialAdjustCls.FSpecialAdjustClsNo = '{0}' ", FSpecialAdjustName);
            }

            if (!string.IsNullOrEmpty(rKRQBeginDate) || !string.IsNullOrEmpty(rKRQEndDate))
            {
                var begin = string.IsNullOrEmpty(rKRQBeginDate) ? DateTime.MinValue.ToString() : rKRQBeginDate;
                var end = string.IsNullOrEmpty(rKRQEndDate) ? DateTime.MaxValue.ToString() : rKRQEndDate;
                filter += string.Format(" AND FInsDate <= '{0}'", end);
                if (!string.IsNullOrEmpty(rKRQBeginDate))
                    filter += string.Format(" and FInsDate >= '{0}'", begin);
            }
            if (!string.IsNullOrEmpty(sSRQBeginDate) || !string.IsNullOrEmpty(sSRQEndDate))
            {
                var begin = string.IsNullOrEmpty(sSRQBeginDate) ? "0" : sSRQBeginDate;
                var end = sSRQEndDate;
                filter += string.Format(" and FAdjustAmount >= '{0}'", begin);
                if (!string.IsNullOrEmpty(sSRQEndDate))
                    filter += string.Format(" and FAdjustAmount <= '{0}'", end);
            }

            return filter;

        }



        public DataTable GetVSBZSXX()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.SpecificAdjust().GetQueryList(strWhere).Tables[0];
            return zsxx;
        }

        public List<Model.SpecificAdjust> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            var zsxx = new BLL.SpecificAdjust().GetModelQueryList(strWhere);
            return zsxx;
        }
        [HttpPost]
        public ActionResult Index(string id)
        {
            var vsbzsxx = GetModelVSBZSXX();
           // return View(vsbzsxx);
            return View();
        }

        [HttpGet]
        public ActionResult Index()
        {
            GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
          //  return View(vsbzsxx);
            return View();
        }

        public ActionResult QuerySpecificAdjustListPartial()
        {
            GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            return View(vsbzsxx);
        }

        #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            var table = GetModelVSBZSXX();
            return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("特殊调整查询导出.xls"), true);
        }

        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "FSpecificAdjustNo";
            setting.CallbackRouteValues = new { Controller = "QuerySpecificAdjust", Action = "QuerySpecificAdjustListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;

            setting.Columns.Add("NSRSBH", "纳税人识别号");
            setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRMC", "企业名称");
            setting.Columns.Add("FSpecialAdjustName", "调整类型");
            setting.Columns.Add("FEmpName", "调整人员");
            setting.Columns.Add("FAdjustAmount", "调整金额");
            setting.Columns.Add("FNote", "备注");
            setting.Columns.Add("FInsDateStr", "调整日期").CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "NSRSBH").DisplayFormat = "总计:";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAdjustAmount").DisplayFormat = "￥{0}";
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAdjustAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";
            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };
            setting.SettingsBehavior.EnableCustomizationWindow = true;
			
            setting.PreRender = setting.BeforeGetCallbackResult = (sender, e) =>
            {


                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QuerySpecificAdjust");


            };
            return setting;
        }

        #endregion

    }
}
